package com.minio.demo.controller;

import com.minio.demo.utils.MinioUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class TestController {

    @Value("${minio.bucketName}")
    private String BUCKET_NAME;

    // 在桶中的文件路径
    @Value("${minio.storePath}")
    private String storePath;

    @Autowired
    private MinioUtils minioUtils;

    @ResponseBody
    @PostMapping("jpg")
    public String uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
        String originalFilename = file.getOriginalFilename();
        if(originalFilename==null || originalFilename.equals("")){
            throw new RuntimeException("请先选择需要上传的文件");
        }
        System.out.println(file.getContentType());
        // TODO MultipartFile 的 InputStream 只能读取一次
        return minioUtils.uploadInputStream(
                BUCKET_NAME, storePath , originalFilename, file.getContentType() ,
                file.getInputStream());
    }

    @GetMapping("/index")
    public String index(){
        return "index" ;
    }

}
